package com.compmov.libraries;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.compmov.datamodel.Lugar;
import com.compmov.datamodel.LugarAgregado;
import com.compmov.socialnetwork.Pestanias;
import com.compmov.socialnetwork.R;

public class LugaresAdapter extends BaseAdapter {
    
    private Activity activity;
    private List<LugarAgregado> data;
    
    private static LayoutInflater inflater=null;
    public ImageLoader imageLoader; 
    
    public LugaresAdapter(Activity a, List<LugarAgregado> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        imageLoader=new ImageLoader(activity.getApplicationContext());
    }

    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        return position;
    }

    public long getItemId(int position) {
        return position;
    }
    
    public static class ViewHolder{
        public TextView text;
        public TextView textA;
        public ImageView image;
        public ImageView imageRan;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ViewHolder holder;
        if(convertView==null){
            vi = inflater.inflate(R.layout.item_lugares, null);
            holder=new ViewHolder();
            holder.text=(TextView)vi.findViewById(R.id.text);
            holder.textA=(TextView)vi.findViewById(R.id.textA);
            holder.image=(ImageView)vi.findViewById(R.id.image);
            holder.imageRan=(ImageView)vi.findViewById(R.id.imageRanking);
            vi.setTag(holder);
        }
        else
            holder=(ViewHolder)vi.getTag();
        
        LugarAgregado lugA=data.get(position);
        Lugar lug=lugA.getLugar();
        holder.text.setText(lug.getNombre());
        holder.textA.setText(lugA.getFecha());
        //Lugar Falta Ranking
        int ranking=lug.getRanking();
        switch(ranking){
        case 2: holder.imageRan.setImageResource(R.drawable.ranking2); break;
        case 3: holder.imageRan.setImageResource(R.drawable.ranking3); break;
        case 4: holder.imageRan.setImageResource(R.drawable.ranking4); break;
        case 5: holder.imageRan.setImageResource(R.drawable.ranking5); break;
        default: holder.imageRan.setImageResource(R.drawable.ranking1); 
        }
        holder.image.setTag(lug.getUrlPrincipal());
        imageLoader.DisplayImage(lug.getUrlPrincipal(), activity, holder.image);
        
        holder.image.setId(position);
        holder.image.setOnClickListener(new View.OnClickListener() {
        	
        	public void onClick(View view) {
        		LugarAgregado lugA=data.get(view.getId());
                Lugar lug=lugA.getLugar();
        		Intent nuevoIntent = new Intent(activity,
						Pestanias.class);
				nuevoIntent.putExtra("id_lugar", ""+lug.getId());
				nuevoIntent.putExtra("nombre", lug.getNombre());
				nuevoIntent.putExtra("ranking", ""+lug.getRanking());
				nuevoIntent.putExtra("descripcion", lug.getDescripcion());
				nuevoIntent.putExtra("url", lug.getUrlPrincipal());
				nuevoIntent.putExtra("address", "");
				activity.startActivity(nuevoIntent);
				
		}
        });
        
        
        return vi;
    }
}